/*
 * @Author: ZhanXie
 * @Date: 2020-09-16 22:19:27
 * @LastEditors: ZhanXie
 * @LastEditTime: 2020-09-17 12:54:27
 * @Description: 微博 @ 关系 controller
 * @FilePath: /weibo/src/controller/blog-at.js
 */

const {
  getAtRelationCount,
  getAtUserBlogList,
  updateAtRelation
} = require('../services/at_relation')
const { SuccessModel } = require('../model/ResModel')
const { PAGE_SIZE } = require('../conf/const')

/**
 * 获取 @ 我的微博数量
 * @param {number} userId userId 
 */
async function getAtMeCount(userId) {
  const count = await getAtRelationCount(userId)
  return new SuccessModel({
    count
  })
}

/**
 * 获取 @ 用户的微博列表
 * @param {number} userId user id 
 * @param {number} pageIndex page index
 */
async function getAtMeBlogList(userId, pageIndex = 0) {
  console.log(pageIndex)
  const result = await getAtUserBlogList({
    userId,
    pageIndex,
    pageSize: PAGE_SIZE
  })
  const { count, blogList } = result

  // 返回
  return new SuccessModel({
    isEmpty: blogList.length === 0,
    blogList,
    pageSize: PAGE_SIZE,
    pageIndex,
    count
  })
}

/**
 * 标记为已读
 * @param {number} userId userid 
 */
async function markAsRead(userId) {
  try {
    await updateAtRelation({
      newIsRead: true
    }, {
      userId,
      isRead: false
    })
  } catch (error) {
    console.error(error)
  }
  // 不需要返回 SuccessModel 或者 ErrorModel
}

module.exports = {
  getAtMeCount,
  getAtMeBlogList,
  markAsRead
}